import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Schema;

/**
 * Created by ma on 2015/9/22.
 */
public class GreenDaoUtil extends AnAction {
    private PsiClass mClass;
    private int dbVersion = 1;
    private String beanPackage;
    private String daoPackage;

    public void actionPerformed(AnActionEvent event) {
        // TODO: insert action logic here
        Project project = event.getData(PlatformDataKeys.PROJECT);

       String dbv= askDBVieson(project);
        try {
            dbVersion = Integer.valueOf(dbv);
        }catch (Exception e){
            Messages.showErrorDialog(project, "输入 >0 的数字", "错误的数据库版本号");
            return;
        }



        Object nav = event.getData(PlatformDataKeys.NAVIGATABLE);
        if (nav instanceof PsiDirectory) {

            PsiDirectory directory = (PsiDirectory) nav;

            if (!directory.isDirectory()) {
                return;
            }


            if (directory.getFiles().length > 0) {
                if (directory.getFiles()[0] instanceof PsiJavaFile) {
                    beanPackage = ((PsiJavaFile) (directory.getFiles()[0])).getPackageName();
                    PsiDirectory daoPsiDirectory = directory.getParent().findSubdirectory("dao");
                    if (daoPsiDirectory == null) {
                        daoPsiDirectory = directory.getParent().createSubdirectory("dao");

                    }

                    daoPackage = beanPackage.replace(directory.getName(), "dao");


                    Schema schema = new Schema(dbVersion, beanPackage);
                    schema.setDefaultJavaPackageDao(daoPackage);
                    WriteTool writeTool = new WriteTool();
                    //
                    PsiFile[] fs = directory.getFiles();
                    for (PsiFile file : fs) {
                        if (file instanceof PsiJavaFile) {

                            writeTool.initBean(schema, file);
                        }

                    }


                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String path = directory.getVirtualFile().getCanonicalPath();
                                String packagebeanpath = beanPackage.replace('.', '/');
                                if (path.endsWith(packagebeanpath)) {
                                    path = path.replace(packagebeanpath, "");
                                    new DaoGenerator().generateAll(schema, path);
                                    if (project != null) {
                                        ProjectView.getInstance(project).refresh();
                                    }
                                }


                            } catch (Exception e) {
                                e.printStackTrace();
                                System.err.print(e.getLocalizedMessage());
                            }
                        }
                    };

                    runnable.run();


                }

            }


        }
    }
    private String askDBVieson(Project project) {

      return     Messages.showInputDialog(project,
              "数据库版本", " ",
              Messages.getQuestionIcon(), "1",  null);
    }


}
